package com.sfdk.structure
{
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.utils.IExternalizable;

[RemoteClass(alias="flex.messaging.io.ArrayCollection")]
	public class ArrayList implements IExternalizable
	{
		public var list_ : Array;
		private var type_ : Class;
		private var size_ : int;
		
		public function ArrayList(numElements:int=0, type : Class = null)
		{
			list_ = new Array(numElements);
			size_ = numElements;
			type_ = type;
		}
		
		public function contains(element : Object) : Boolean
		{
			var index : int = list_.indexOf(element, 0);
			return index != -1;
		}
		
		public function addItem(element : Object) : void
		{
			list_.push(element);
			size_++;
		}
		
		public function getItemAt(index  : int) : Object
		{
			return list_[index];
		}
		
		public function getByName(name : String, classElement : Class) : Object
		{
			for(var i : int = 0; i < size_ ; i++)
			{
				if((list_[i] as classElement).getName() == name)
				{
					return list_[i];
				}
			}
			return null;	
		}
		
		public function getItemIndex(element : Object) : int
		{
			return list_.indexOf(element);
		}
		
		public function removeItemAt(index : int) :void
		{
			list_.splice(index, 1);
			size_--;
		}
		
		public function setItemAt(element : Object, index : int) : void
		{
			list_[index] = element;
		}
		
		public function size() : int
		{
			return size_;
		}
		
		public function removeLast() : Object
		{
			if(size_ > 0)
				size_--;
			return list_.shift();			
		}
		
		public function toArray() : Array
		{
			return list_;
		}
		
		public function removeAll() : void
		{
			list_ = new Array();
			size_ = 0;
			type_ = null;
		}
		
		public function readExternal(input:IDataInput):void
		{
			list_ = input.readObject() as Array;
		}
		
		/**
		 *  @private
		 *  Ensures that only the source property is serialized.
		 */
		public function writeExternal(output:IDataOutput):void
		{
			output.writeObject(list_);
		}
	}
}